package org.jeecg.modules.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.entity.IntegrationStandard;
import org.jeecg.modules.system.service.IIntegrationStandardService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 集成规范管理
 * @Author: jeecg-boot
 * @Date: 2024-07-16
 * @Version: V1.0
 */
@Api(tags = "集成规范管理")
@RestController
@RequestMapping("/integrationStandard")
@Slf4j
public class IntegrationStandardController extends JeecgController<IntegrationStandard, IIntegrationStandardService> {
    @Resource
    private IIntegrationStandardService integrationStandardService;

    /**
     * 分页列表查询
     *
     * @param integrationStandard
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "集成规范管理-分页列表查询")
    @ApiOperation(value = "集成规范管理-分页列表查询", notes = "集成规范管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<IntegrationStandard>> queryPageList(IntegrationStandard integrationStandard,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                            HttpServletRequest req) {
        QueryWrapper<IntegrationStandard> queryWrapper = QueryGenerator.initQueryWrapper(integrationStandard, req.getParameterMap());
        queryWrapper.eq("del_flag",CommonConstant.STATUS_NORMAL.toString());
        Page<IntegrationStandard> page = new Page<IntegrationStandard>(pageNo, pageSize);
        IPage<IntegrationStandard> pageList = integrationStandardService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param integrationStandard
     * @return
     */
    @AutoLog(value = "集成规范管理-添加")
    @ApiOperation(value = "集成规范管理-添加", notes = "集成规范管理-添加")
    @RequiresPermissions("standard:integration_standard:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody IntegrationStandard integrationStandard) {
        try {
            String[] split = integrationStandard.getStandardFilename().split("/");
            integrationStandard.setStandardFilepath(integrationStandard.getStandardFilename());
            integrationStandard.setStandardFilename(split[split.length - 1]);
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            integrationStandard.setOrgcode(loginUser.getOrgCode());
            integrationStandard.setDelFlag(CommonConstant.STATUS_NORMAL.toString());
            integrationStandardService.save(integrationStandard);
            return Result.OK("添加成功！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 编辑
     *
     * @param integrationStandard
     * @return
     */
    @AutoLog(value = "集成规范管理-编辑")
    @ApiOperation(value = "集成规范管理-编辑", notes = "集成规范管理-编辑")
    @RequiresPermissions("standard:integration_standard:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody IntegrationStandard integrationStandard) {
        if (StringUtils.isNotEmpty(integrationStandard.getStandardFilename())) {
            String[] split = integrationStandard.getStandardFilename().split("/");
            integrationStandard.setStandardFilepath(integrationStandard.getStandardFilename());
            integrationStandard.setStandardFilename(split[split.length - 1]);
        }

        integrationStandardService.updateById(integrationStandard);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "集成规范管理-通过id删除")
    @ApiOperation(value = "集成规范管理-通过id删除", notes = "集成规范管理-通过id删除")
    @RequiresPermissions("standard:integration_standard:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        LambdaUpdateWrapper<IntegrationStandard> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(IntegrationStandard::getDelFlag,CommonConstant.DEL_FLAG_1.toString());
        updateWrapper.eq(IntegrationStandard::getId,id);
        integrationStandardService.update(updateWrapper);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "集成规范管理-批量删除")
    @ApiOperation(value = "集成规范管理-批量删除", notes = "集成规范管理-批量删除")
    @RequiresPermissions("standard:integration_standard:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.integrationStandardService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "集成规范管理-通过id查询")
    @ApiOperation(value = "集成规范管理-通过id查询", notes = "集成规范管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<IntegrationStandard> queryById(@RequestParam(name = "id", required = true) String id) {
        IntegrationStandard integrationStandard = integrationStandardService.getById(id);
        if (integrationStandard == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(integrationStandard);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param integrationStandard
     */
    @RequiresPermissions("standard:integration_standard:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, IntegrationStandard integrationStandard) {
        return super.exportXls(request, integrationStandard, IntegrationStandard.class, "集成规范管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("standard:integration_standard:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, IntegrationStandard.class);
    }

}
